package de.revdev.llt.bean.managed;

import de.revdev.llt.bean.entities.FileCard;
import de.revdev.llt.bean.entities.FileCardBox;
import de.revdev.llt.bean.entities.History;
import de.revdev.llt.bean.facade.FileCardBoxFacade;
import de.revdev.llt.bean.facade.FileCardFacade;
import de.revdev.llt.bean.facade.HistoryFacade;
import java.io.Serializable;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "fCBViewBean")
@SessionScoped
public class FCBViewBean implements Serializable {

    private ShowClassRoomBean showClassRoomBean;
    private FileCardFacade fcf;
    private FileCardBoxFacade fcbf;
    private UserBean userBean;
    private HistoryFacade hf;
    private FileCardBox fcb;
    private FileCard fc;
    private History history;
    private FileCard sfc;
    private static final Logger log = Logger.getLogger(FCBViewBean.class.getName());

    /**
     * Creates a new instance of FCBViewBean
     */
    public FCBViewBean() {
    }

    @PostConstruct
    private void init() {
        fc = new FileCard();
        history = new History();
    }

    public String openFileCardBox(FileCardBox fileCardBox) {
        this.fcb = fileCardBox;
        return "/faces/user/filecardbox.xhtml?faces-redirect=true";
    }

    public void open(ActionEvent event) {
        long id = (long) event.getComponent().getAttributes().get("fcbid");
        this.fcb = fcbf.find(id);
        userBean.setClassRoomTemplate("/faces/user/editfilecards.xhtml");
    }

    public void saveFileCard() {
        fc.setLastModified(Calendar.getInstance().getTime());
        if (fc.getId() != null) {
            if (!fc.getAnswer().equals(history.getAnswer()) || !fc.getQuestion().equals(history.getQuestion())) {
                hf.create(history);
                fc.getHistory().add(history);
                log.log(Level.INFO, "History {0} for FileCard {1} was created", new Object[]{history.getId(), fc.getId()});
            }
        } else {
            fc.setFileCardBox(fcb);
            fcf.create(fc);
            fcb.getFileCards().add(fc);
            log.log(Level.INFO, "FileCard {0} for FileCardBox {1} was created", new Object[]{fc.getId(), fcb.getId()});
        }
        fcbf.edit(fcb);
        fc = new FileCard();
        history = new History();
    }

    public void deleteFileCard() {
        fcb.getFileCards().remove(fc);
        fcf.remove(fc);
        log.log(Level.INFO, "FileCard {0} was deleted", fc.getId());
        fc = new FileCard();
        history = new History();
    }

    public void newFileCard() {
        fc = new FileCard();
        history = new History();
    }

    public void cancelFileCard() {
        fc = new FileCard();
        history = new History();
    }

    public void onRowSelect(SelectEvent event) {
        fc = (FileCard) event.getObject();
        history = new History(fc);
    }

    public void onRowUnselect(UnselectEvent event) {
        fc = new FileCard();
        history = new History();
    }

    @Inject
    public void setFileCardFacade(FileCardFacade fielCardFacade) {
        this.fcf = fielCardFacade;
    }

    @Inject
    public void setFileCardBoxFacade(FileCardBoxFacade fielCardBoxFacade) {
        this.fcbf = fielCardBoxFacade;
    }

    @Inject
    public void setHistoryFacade(HistoryFacade historyFacade) {
        this.hf = historyFacade;
    }

    @Inject
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    @Inject
    public void setShowClassRoomBean(ShowClassRoomBean showClassRoomBean) {
        this.showClassRoomBean = showClassRoomBean;
    }

    public FileCardBox getFcb() {
        return fcb;
    }

    public void setFcb(FileCardBox fcb) {
        this.fcb = fcb;
    }

    public FileCard getFc() {
        return fc;
    }

    public void setFc(FileCard fc) {
        this.fc = fc;
    }

    public FileCard getSfc() {
        return sfc;
    }

    public void setSfc(FileCard sfc) {
        this.sfc = sfc;
    }
}